package com.cl.mybatis.learn.day06;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.ibatis.type.EnumOrdinalTypeHandler;
import org.apache.ibatis.type.EnumTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;

/**
 * Enabled类型处理器
 */
public class EnabledTypeHandler2 extends EnumOrdinalTypeHandler<Enabled> {
    private final Map<Integer, Enabled> enabledMap = new HashMap<Integer, Enabled>();
    
    public EnabledTypeHandler2(Class<Enabled> type) {
        super(type);
        for (Enabled enabled : Enabled.values()) {
            enabledMap.put(enabled.getCode(), enabled);
        }
    }
    
    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, Enabled enabled, JdbcType jdbcType)
            throws SQLException {
        preparedStatement.setInt(i, enabled.getCode());
    }
    
    @Override
    public Enabled getResult(ResultSet resultSet, String columnName) throws SQLException {
        Integer value = resultSet.getInt(columnName);
        return enabledMap.get(value);
    }
    
    @Override
    public Enabled getResult(ResultSet resultSet, int columnIndex) throws SQLException {
        Integer value = resultSet.getInt(columnIndex);
        return enabledMap.get(value);
    }
    
    @Override
    public Enabled getResult(CallableStatement callableStatement, int columnIndex) throws SQLException {
        Integer value = callableStatement.getInt(columnIndex);
        return enabledMap.get(value);
    }
}